package com.cwg.dict.ui.rcp.views;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;

import com.cwg.dict.model.History;
import com.cwg.dict.service.impl.HistoricalImpl;
import com.cwg.dict.service.impl.NearWordSelector;
import com.cwg.dict.stem.model.Token;
import com.cwg.dict.ui.rcp.Activator;

public class WordListView extends ViewPart {
	public static final String	ID	= "com.cwg.dict.WordListView";
	private ComboViewer			comboViewer;
	private NearWordSelector	nearWordSelector;
	private ListViewer			wordlistView;

	@Override
	public void createPartControl(Composite parent) {
		parent.setLayout(new GridLayout(2, false));
		Composite c = new Composite(parent, SWT.NONE);
		c.setLayout(new FillLayout());
		c.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL));
		initComboViewer(c);

		Button button = new Button(parent, SWT.PUSH);
		button.setText(" Go ");
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				query();
			}
		});
		GridData gridData = new GridData(GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL);
		gridData.horizontalSpan = 2;
		Composite c2 = new Composite(parent, SWT.NONE);
		c2.setLayout(new FillLayout());
		c2.setLayoutData(gridData);
		initWordListView(c2);
		nearWordSelector = new NearWordSelector(wordlistView.getList().getItems());
	}

	public ComboViewer getComboViewer() {
		return comboViewer;
	}

	@Override
	public void setFocus() {
		// do nothing
	}

	private void addToHistory(String word) {
		HistoricalImpl historical = Activator.getDefault().getHistorical();
		historical.add(word);
		historical.save();
	}

	private void initComboViewer(Composite c) {
		comboViewer = new ComboViewer(c, SWT.BORDER);
		comboViewer.setContentProvider(new ArrayContentProvider() {
			@Override
			public Object[] getElements(Object inputElement) {
				List<History> all = (List<History>) inputElement;
				return all.toArray(new History[all.size()]);
			}
		});
		comboViewer.setLabelProvider(new LabelProvider() {
			@Override
			public String getText(Object element) {
				History his = (History) element;
				return his.getWord();
			}
		});
		comboViewer.setInput(new ArrayList<History>());

		comboViewer.getCombo().addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(final KeyEvent e) {
				if (e.keyCode == SWT.CR || e.keyCode == SWT.LF) {
					query();
				}
			}

			@Override
			public void keyReleased(KeyEvent e) {
				super.keyReleased(e);
				String word = comboViewer.getCombo().getText();
				if (word.length() > 0) {
					String nearWord = nearWordSelector.selectWordNearTo(word);
					wordlistView.getList().setSelection(new String[] { nearWord });
				}
			}

		});
	}

	private void initWordListView(Composite c2) {
		wordlistView = new ListViewer(c2);
		wordlistView.setContentProvider(new ArrayContentProvider());
		wordlistView.setLabelProvider(new LabelProvider());

		String[] wordList = Activator.getDefault().getDictManager().getWordList("");
		wordlistView.setInput(wordList);
		final ListViewer wordListListViewer2 = wordlistView;
		wordlistView.getList().addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				super.mouseDown(e);
				comboViewer.getCombo().setText(wordListListViewer2.getList().getSelection()[0]);
				query();
			}
		});
	}

	private void queryInNetDict(String word) {
		String[] dictNames = Activator.getDefault().getNetDictNames();
		for (String each : dictNames) {
			DictionaryView view = (DictionaryView) Activator.getDefault().findViewreference(DictionaryView.ID, each).getView(true);
			String url = Activator.getDefault().getNetDictQueryService().makeQueryUrl(each, word);
			view.setUrl(url);
		}
	}

	private void queryInStemDict(String word) {
		Token[] tokens = Activator.getDefault().getWordParser().query(word);
		StemView view = (StemView) Activator.getDefault().findViewreference(StemView.ID).getView(true);
		view.setTokens(tokens);
	}

	protected void query() {
		String word = comboViewer.getCombo().getText();
		queryInStemDict(word);
		queryInNetDict(word);
		addToHistory(word);
	}

}
